package com.example.administrator.szb.activity.supei;

import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.szb.R;
import com.example.administrator.szb.bean.BusinessSuPei;

import java.util.List;

/**
 * Created by stone on 2018/1/23.
 */

public class SuPeiAdapter  extends BaseAdapter {
    private Context context;
    private List<BusinessSuPei> list;
    LayoutInflater inflater;
    public SuPeiAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public  BusinessSuPei getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position,  View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            convertView = inflater.inflate(R.layout.item_supei_edit,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        final View view = convertView;
        holder.reset();
        BusinessSuPei businessSuPei = getItem(position);
        holder.tv_unit.setText(businessSuPei.getUnits());
        holder.tv_title.setText(businessSuPei.getTitle());
        holder.et_title.setHint(businessSuPei.getAskfor());
        switch (businessSuPei.getDatatype()){
            case 1:
            case 4:


                if (businessSuPei.getMold()==0){
                    holder.iv_go.setVisibility(View.VISIBLE);
                    holder.et_title.setVisibility(View.GONE);
                    holder.et_title2.setVisibility(View.VISIBLE);
                    holder.et_title2.setHint(businessSuPei.getAskfor());
                    view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (listener!=null){
                                listener.onItemClick(position,view);
                            }
                        }
                    });
                }else {
                    holder.et_title.setVisibility(View.VISIBLE);
                    holder.et_title.setInputType(InputType.TYPE_CLASS_TEXT);
                    holder.tv_unit.setVisibility(View.VISIBLE);
                    holder.et_title.setFilters(new InputFilter[]{new InputFilter.LengthFilter(businessSuPei.getDataleng())}); //最大输入长度
                }

                break;
            case 2:
                holder.et_title.setVisibility(View.VISIBLE);
                holder.et_title.setInputType(InputType.TYPE_CLASS_NUMBER);
                holder.et_title.setFilters(new InputFilter[]{new InputFilter.LengthFilter(businessSuPei.getDataleng())}); //最大输入长度
                holder.tv_unit.setVisibility(View.VISIBLE);
                break;
            case 3:
                holder.et_title.setVisibility(View.VISIBLE);
                holder.et_title.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
                holder.et_title.setFilters(new InputFilter[]{new InputFilter.LengthFilter(businessSuPei.getDataleng())}); //最大输入长度
                holder.tv_unit.setVisibility(View.VISIBLE);
                break;
        }
        if (businessSuPei.getMold()==1){
            holder.et_title.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (listener!=null){
                        listener.onEditChange(position,s.toString());
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        }


        return convertView;
    }

    class ViewHolder{
         TextView tv_title,et_title2;
         EditText et_title;
         TextView tv_unit;
         ImageView iv_go;
        View view;
        public ViewHolder(View view){
            this.view = view;
            tv_title = (TextView) view.findViewById(R.id.tv_title);
            et_title = (EditText) view.findViewById(R.id.et_title);
            tv_unit = (TextView) view.findViewById(R.id.tv_unit);
            iv_go = (ImageView) view.findViewById(R.id.iv_go);
            et_title2 = view.findViewById(R.id.et_title2);
        }
        public void reset(){
            et_title.setVisibility(View.GONE);
            tv_unit.setVisibility(View.GONE);
            iv_go.setVisibility(View.GONE);
            et_title2.setVisibility(View.GONE);
            this.view.setOnClickListener(null);
        }
    }
    OnItemClickListener listener;

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener{
        void onItemClick(int pos, View view);
        void onEditChange(int pos, String str);
    }
}